package com.linkoog.devtools.archive.filesystem.tar;

import com.linkoog.devtools.archive.filesystem.ArchiveHandlerBase;
import com.intellij.util.io.FileAccessorCache;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Objects;

/**
 */
public class TarFileHandler extends ArchiveHandlerBase<TarFile> {

    public TarFileHandler(@NotNull String path) {
        super(path);
    }

    @Override
    public FileAccessorCache<TarFileHandler, TarFile> getFileAccessor() {
        return fileAccessor;
    }

    private static final FileAccessorCache<TarFileHandler, TarFile> fileAccessor = new FileAccessorCache<TarFileHandler, TarFile>(20, 10) {

        @Override
        protected @NotNull TarFile createAccessor(TarFileHandler key) throws IOException {
            setFileAttributes(key, key.getCanonicalPath());
            return new TarFile(key.getFile().getCanonicalFile());
        }

        @Override
        public boolean isEqual(TarFileHandler val1, TarFileHandler val2) {
            return Objects.equals(val1, val2);
        }

        @Override
        protected void disposeAccessor(@NotNull TarFile fileAccessor) throws IOException {
            fileAccessor.close();
        }

    };

}